BUG#1244 Use a UNION to improve index use.
[lhc/web/wiklou.git] / includes / Block.php
1 <?php
2 /**
3 * Blocks and bans object
4 * @package MediaWiki
5 */
6
7 /**
8 * Some globals
9 */
10 define ( 'EB_KEEP_EXPIRED', 1 );
11 define ( 'EB_FOR_UPDATE', 2 );
12
13 /**
14 * The block class
15 * All the functions in this class assume the object is either explicitly
16 * loaded or filled. It is not load-on-demand. There are no accessors.
17 *
18 * To use delete(), you only need to fill $mAddress
19 * Globals used: $wgBlockCache, $wgAutoblockExpiry
20 *
21 * @todo This could be used everywhere, but it isn't.
22 * @package MediaWiki
23 */
24 class Block
25 {
26 /* public*/ var $mAddress, $mUser, $mBy, $mReason, $mTimestamp, $mAuto, $mId, $mExpiry;
27 /* private */ var $mNetworkBits, $mIntegerAddr, $mForUpdate;
28
29 function Block( $address = '', $user = '', $by = 0, $reason = '',
30 $timestamp = '' , $auto = 0, $expiry = '' )
31 {
32 $this->mAddress = $address;
33 $this->mUser = $user;
34 $this->mBy = $by;
35 $this->mReason = $reason;
36 $this->mTimestamp = wfTimestamp(TS_MW,$timestamp);
37 $this->mAuto = $auto;
38 $this->mExpiry = wfTimestamp(TS_MW,$expiry);
39
40 $this->mForUpdate = false;
41 $this->initialiseRange();
42 }
43
44 /*static*/ function newFromDB( $address, $user = 0, $killExpired = true )
45 {
46 $ban = new Block();
47 $ban->load( $address, $user, $killExpired );
48 return $ban;
49 }
50
51 function clear()
52 {
53 $mAddress = $mReason = $mTimestamp = '';
54 $mUser = $mBy = 0;
55 }
56
57 # Get a ban from the DB, with either the given address or the given username
58 function load( $address = '', $user = 0, $killExpired = true )
59 {
60 $fname = 'Block::load';
61
62 $ret = false;
63 $killed = false;
64 if ( $this->forUpdate() ) {
65 $db =& wfGetDB( DB_MASTER );
66 $options = 'FOR UPDATE';
67 } else {
68 $db =& wfGetDB( DB_SLAVE );
69 $options = '';
70 }
71 $ipblocks = $db->tableName( 'ipblocks' );
72
73 if ( 0 == $user && $address=='' ) {
74 $sql = "SELECT * from $ipblocks $options";
75 } elseif ($address=="") {
76 $sql = "SELECT * FROM $ipblocks WHERE ipb_user={$user} $options";
77 } elseif ($user=="") {
78 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) . "' $options";
79 } elseif ( $options=='' ) {
80 # If there are no optiones (e.g. FOR UPDATE), use a UNION
81 # so that the query can make efficient use of indices
82 $sql = "SELECT * FROM $ipblocks WHERE ipb_address='" . $db->strencode( $address ) .
83 "' UNION SELECT * FROM $ipblocks WHERE ipb_user={$user}";
84 } else {
85 # If there are options, a UNION can not be used, use one
86 # SELECT instead. Will do a full table scan.
87 $sql = "SELECT * FROM $ipblocks WHERE (ipb_address='" . $db->strencode( $address ) .
88 "' OR ipb_user={$user}) $options";
89 }
90
91 $res = $db->query( $sql, $fname );
92 if ( 0 == $db->numRows( $res ) ) {
93 # User is not blocked
94 $this->clear();
95 } else {
96 # Get first block
97 $row = $db->fetchObject( $res );
98 $this->initFromRow( $row );
99
100 if ( $killExpired ) {
101 # If requested, delete expired rows
102 do {
103 $killed = $this->deleteIfExpired();
104 if ( $killed ) {
105 $row = $db->fetchObject( $res );
106 if ( $row ) {
107 $this->initFromRow( $row );
108 }
109 }
110 } while ( $killed && $row );
111
112 # If there were any left after the killing finished, return true
113 if ( !$row ) {
114 $ret = false;
115 $this->clear();
116 } else {
117 $ret = true;
118 }
119 } else {
120 $ret = true;
121 }
122 }
123 $db->freeResult( $res );
124 return $ret;
125 }
126
127 function initFromRow( $row )
128 {
129 $this->mAddress = $row->ipb_address;
130 $this->mReason = $row->ipb_reason;
131 $this->mTimestamp = wfTimestamp(TS_MW,$row->ipb_timestamp);
132 $this->mUser = $row->ipb_user;
133 $this->mBy = $row->ipb_by;
134 $this->mAuto = $row->ipb_auto;
135 $this->mId = $row->ipb_id;
136 $this->mExpiry = wfTimestamp(TS_MW,$row->ipb_expiry);
137
138 $this->initialiseRange();
139 }
140
141 function initialiseRange()
142 {
143 if ( $this->mUser == 0 ) {
144 $rangeParts = explode( '/', $this->mAddress );
145 if ( count( $rangeParts ) == 2 ) {
146 $this->mNetworkBits = $rangeParts[1];
147 } else {
148 $this->mNetworkBits = 32;
149 }
150 $this->mIntegerAddr = ip2long( $rangeParts[0] );
151 } else {
152 $this->mNetworkBits = false;
153 $this->mIntegerAddr = false;
154 }
155 }
156
157 # Callback with a Block object for every block
158 /*static*/ function enumBlocks( $callback, $tag, $flags = 0 )
159 {
160 $block = new Block();
161 if ( $flags & EB_FOR_UPDATE ) {
162 $db =& wfGetDB( DB_MASTER );
163 $options = 'FOR UPDATE';
164 $block->forUpdate( true );
165 } else {
166 $db =& wfGetDB( DB_SLAVE );
167 $options = '';
168 }
169 $ipblocks = $db->tableName( 'ipblocks' );
170
171 $sql = "SELECT * FROM $ipblocks ORDER BY ipb_timestamp DESC $options";
172 $res = $db->query( $sql, 'Block::enumBans' );
173
174 while ( $row = $db->fetchObject( $res ) ) {
175 $block->initFromRow( $row );
176 if ( !( $flags & EB_KEEP_EXPIRED ) ) {
177 if ( !$block->deleteIfExpired() ) {
178 $callback( $block, $tag );
179 }
180 } else {
181 $callback( $block, $tag );
182 }
183 }
184 wfFreeResult( $res );
185 }
186
187 function delete()
188 {
189 $fname = 'Block::delete';
190 $dbw =& wfGetDB( DB_MASTER );
191
192 if ( $this->mAddress == '' ) {
193 $condition = array( 'ipb_id' => $this->mId );
194 } else {
195 $condition = array( 'ipb_address' => $this->mAddress );
196 }
197 $dbw->delete( 'ipblocks', $condition, $fname );
198 $this->clearCache();
199 }
200
201 function insert()
202 {
203 $dbw =& wfGetDB( DB_MASTER );
204 $dbw->insert( 'ipblocks',
205 array(
206 'ipb_address' => $this->mAddress,
207 'ipb_user' => $this->mUser,
208 'ipb_by' => $this->mBy,
209 'ipb_reason' => $this->mReason,
210 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
211 'ipb_auto' => $this->mAuto,
212 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
213 ), 'Block::insert'
214 );
215
216 $this->clearCache();
217 }
218
219 function deleteIfExpired()
220 {
221 if ( $this->isExpired() ) {
222 $this->delete();
223 return true;
224 } else {
225 return false;
226 }
227 }
228
229 function isExpired()
230 {
231 if ( !$this->mExpiry ) {
232 return false;
233 } else {
234 return wfTimestamp() > $this->mExpiry;
235 }
236 }
237
238 function isValid()
239 {
240 return $this->mAddress != '';
241 }
242
243 function updateTimestamp()
244 {
245 if ( $this->mAuto ) {
246 $this->mTimestamp = wfTimestamp();
247 $this->mExpiry = Block::getAutoblockExpiry( $this->mTimestamp );
248
249 $dbw =& wfGetDB( DB_MASTER );
250 $dbw->update( 'ipblocks',
251 array( /* SET */
252 'ipb_timestamp' => $dbw->timestamp($this->mTimestamp),
253 'ipb_expiry' => $dbw->timestamp($this->mExpiry),
254 ), array( /* WHERE */
255 'ipb_address' => $this->mAddress
256 ), 'Block::updateTimestamp'
257 );
258
259 $this->clearCache();
260 }
261 }
262
263 /* private */ function clearCache()
264 {
265 global $wgBlockCache;
266 if ( is_object( $wgBlockCache ) ) {
267 $wgBlockCache->loadFromDB();
268 }
269 }
270
271 function getIntegerAddr()
272 {
273 return $this->mIntegerAddr;
274 }
275
276 function getNetworkBits()
277 {
278 return $this->mNetworkBits;
279 }
280
281 function forUpdate( $x = NULL ) {
282 return wfSetVar( $this->mForUpdate, $x );
283 }
284
285 /* static */ function getAutoblockExpiry( $timestamp )
286 {
287 global $wgAutoblockExpiry;
288 return wfTimestamp( TS_MW, wfTimestamp( TS_UNIX, $timestamp ) + $wgAutoblockExpiry );
289 }
290
291 /* static */ function normaliseRange( $range )
292 {
293 $parts = explode( '/', $range );
294 if ( count( $parts ) == 2 ) {
295 $shift = 32 - $parts[1];
296 $ipint = ip2long( $parts[0] );
297 $ipint = $ipint >> $shift << $shift;
298 $newip = long2ip( $ipint );
299 $range = "$newip/{$parts[1]}";
300 }
301 return $range;
302 }
303
304 }
305 ?>